package com.avcdata.aoscustommade.model;

import android.content.Context;

import com.avcdata.aoscustommade.activity.CityDatail_Activity;
import com.avcdata.aoscustommade.bean.DateSelectCategoryBean;
import com.avcdata.aoscustommade.bean.DetailsBean;
import com.avcdata.aoscustommade.bean.HotBrandCumulativeBean;
import com.avcdata.aoscustommade.bean.TechnologyBean;
import com.avcdata.aoscustommade.main.BaseCallBack;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.TextUitl;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zyh on 17/6/7.
 */

public class CityDatailActivityModel {
    private final Context mContext;

    public CityDatailActivityModel(Context context) {
        this.mContext = context;
    }

    /**
     * 获取产品类型的日期
     * @param type
     * @param lastWeekCount
     * @param baseCallBack
     */
    public void loadSelectCategory(String type, int lastWeekCount, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", type);
            jsonObject.put("lastWeekCount", lastWeekCount);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderSelectInfo + "getDateSelectCategory", jsonObject, new AvcJsonHttpResponseHandler(mContext, DateSelectCategoryBean.class){

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                DateSelectCategoryBean bean = (DateSelectCategoryBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 获取价格段＋容积段＋渠道
     * @param categoryString
     * @param userLimits
     * @param lastWeek
     * @param cityString
     * @param isTabTime
     * @param lastWeekCount
     * @param time_startString
     * @param time_endString
     * @param accumulated
     * @param baseCallBack
     */
    public void loadDatail(String categoryString, String userLimits, String lastWeek, String cityString, boolean isTabTime, int lastWeekCount, String time_startString, String time_endString, boolean accumulated, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
//            Log.e("string","-------全年请求测试categoryString---------"+categoryString);
//            Log.e("string","-------全年请求测试showWeek---------"+showWeek);
//            Log.e("string","-------全年请求测试sp.getNewWeek())---------"+sp.getNewWeek());
            jsonObject.put("category", categoryString);
            if (userLimits.equals("1")) {
                jsonObject.put("type", "channelName");
            } else {
                jsonObject.put("type", "channelType");
            }
            jsonObject.put("lastWeek", lastWeek);
            if (cityString.equals("全国")) {
                jsonObject.put("cityName", "all");
            } else {
                jsonObject.put("cityName", cityString);
            }

            if (isTabTime) {
                jsonObject.put("lastWeekCount", lastWeekCount);
            } else {
                jsonObject.put("lastWeekCount", TextUitl.getStartEndWeek(time_startString, time_endString));
            }
            jsonObject.put("accumulated", accumulated);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderSelectInfo + "getDate", jsonObject, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject object) {
                super.onSuccess(object);
                baseCallBack.onSuccess(object);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });

    }

    /**
     * 获取热门品牌销售累计情况
     * @param jsonObject
     * @param baseCallBack
     */
    public void loadHotBrandsales(JSONObject jsonObject, final BaseCallBack baseCallBack) {
        AvcHttpUtils.post(AvcConstants.URLHeaderDatail + "queryHotBrandMarketShare", jsonObject, true, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject object) {
                super.onSuccess(object);
                baseCallBack.onSuccess(object);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 获取销售额占有率
     * @param jsonObject
     * @param baseCallBack
     */
    public void loadSalesOccupancy(JSONObject jsonObject, final BaseCallBack baseCallBack) {
        AvcHttpUtils.post(AvcConstants.URLHeaderDatail + "queryHotBrandCumulative", jsonObject, true, new AvcJsonHttpResponseHandler(mContext, HotBrandCumulativeBean.class) {

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                HotBrandCumulativeBean bean = (HotBrandCumulativeBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 获取销售走势图
     * @param object
     * @param baseCallBack
     */
    public void loadSalesTrend(JSONObject object, final BaseCallBack baseCallBack) {
        AvcHttpUtils.post(AvcConstants.URLHeaderDatail + "queryAOCumulative", object, true, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject object) {
                super.onSuccess(object);
                baseCallBack.onSuccess(object);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 获取Top50机型技术类型
     * @param category
     * @param type
     * @param baseCallBack
     */
    public void loadTop50Skilltype(String category, String type, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("category", category);
            jsonObject.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderSelectInfo + "getDateSelectCategory", jsonObject, new AvcJsonHttpResponseHandler(mContext, TechnologyBean.class) {

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                TechnologyBean bean = (TechnologyBean) object;
                baseCallBack.onSuccess(bean);
            }


            @Override
            public void onFailure() {
                super.onFailure();
               baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    //获取top50机型详情
    public void loadTOP50type(JSONObject jsonObject, final BaseCallBack baseCallBack) {
        AvcHttpUtils.post(AvcConstants.URLHeaderrankModelDatail, jsonObject, true, new AvcJsonHttpResponseHandler(mContext, DetailsBean.class) {


            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                DetailsBean bean = (DetailsBean) object;
                baseCallBack.onSuccess(bean);
            }


            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }
}
